package com.czjtxy.extraTerm.serverImpl;

import com.czjtxy.extraTerm.dao.TemployeeDao;
import com.czjtxy.extraTerm.daoImpl.TemployeeDaoImpl;
import com.czjtxy.extraTerm.entity.DO.Temployee;
import com.czjtxy.extraTerm.entity.VO.TemployeeVO;
import com.czjtxy.extraTerm.server.EmployeeService;

import java.sql.SQLException;
import java.util.List;

/**
 * @program: staff-house
 * @author: MingYi
 * @create: 2021/07/01 16:05
 */
public class EmployeeServiceImpl implements EmployeeService {
    TemployeeDao temployeeDao = new TemployeeDaoImpl();

    @Override
    public List<TemployeeVO> getAll() throws SQLException {

        List<TemployeeVO> temployeeVOs = null;
        try {
            temployeeVOs = temployeeDao.getAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            temployeeDao.closeResource();
        }
        return temployeeVOs;
    }

    @Override
    public List<TemployeeVO> getPart(Temployee temployee) throws SQLException {
        List<TemployeeVO> temployeeVOS = null;
        try {
            temployeeVOS = temployeeDao.getPart(temployee);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            temployeeDao.closeResource();
        }
        return temployeeVOS;
    }

    @Override
    public TemployeeVO getOne(int id) throws SQLException {
        TemployeeVO temployeeVO = null;

        try {
            temployeeVO = temployeeDao.getOne(id);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            temployeeDao.closeResource();
        }

        return temployeeVO;
    }

    @Override
    public void updateEmployee(Temployee temployee) throws SQLException {
        try {
            temployeeDao.updateEmployee(temployee);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            temployeeDao.closeResource();
        }
    }

    @Override
    public void deleteEmployee(String ids) throws SQLException {
        try {
            String[] idsArr = ids.split(",");
            for (String str : idsArr) {
                int id = Integer.parseInt(str);
                temployeeDao.deleteEmployee(id);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            temployeeDao.closeResource();
        }
    }

    @Override
    public void addEmployee(Temployee temployee) throws SQLException {
        try {
            temployeeDao.addEmployee(temployee);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            temployeeDao.closeResource();
        }
    }
}